import { Effect, Reducer } from 'umi'

import { IBaseResp } from '@/utils/types'

export interface saveInfoState {
  courseId: string
  courseName: string

  student: {
    studentId: string
    studentName: string
    studentSex: string
    studentNumber: string
    studentGrade: string
    studentClass: string
    studentPhoto: string
  }
}
export interface courseModal {
  namespace: 'saveInfo'
  state: saveInfoState
  effects: {
    courseInfoEffect: Effect
    studentInfoEffect: Effect
  }
  reducers: {
    courseInfo: Reducer<saveInfoState>
    studentInfo: Reducer<saveInfoState>
  }
}
const ICourseModal: courseModal = {
  namespace: 'saveInfo',
  state: {
    courseId: '',
    courseName: '',
    student: {
      studentId: '',
      studentName: '',
      studentSex: '',
      studentNumber: '',
      studentGrade: '',
      studentClass: '',
      studentPhoto: ''
    }
  },
  effects: {
    *courseInfoEffect({ payload, callback }, { call, put }) {
      callback && callback(payload)
      yield put({
        type: 'courseInfo',
        payload
      })
    },
    *studentInfoEffect({ payload, callback }, { call, put }) {
      yield put({
        type: 'studentInfo',
        payload
      })
    }
  },
  reducers: {
    courseInfo(_, { payload }) {
      const _state = JSON.parse(JSON.stringify(_))
      _state.courseId = payload.courseId
      _state.courseName = payload.courseName
      console.log('1')
      return {
        ..._state
      }
    },
    studentInfo(_, { payload }) {
      const _state = JSON.parse(JSON.stringify(_))
      return {
        ..._state,
        student: { ...payload }
      }
    }
  }
}
export default ICourseModal
